/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.commands.ChangeNameCommand;
import edu.pku.sei.transformation.editor.controller.policies.NestedNodeFlowLayoutPolicy;
import edu.pku.sei.transformation.editor.model.GraphicalDomain;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.expression.Expression;
import edu.pku.sei.transformation.editor.model.variable.ObjectVariable;
import edu.pku.sei.transformation.editor.model.variable.Variable;
import edu.pku.sei.transformation.editor.views.figures.ITemplateFigure;
import edu.pku.sei.transformation.editor.views.figures.ObjectVariableFigure;

/**
 * @generated
 * 
 */
public class ObjectVariableEditpart extends NodeTemplateEditpart {
	public ObjectVariableEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	public void activate() {
		super.activate();
		GraphicalDomain domain = getObjectTemplate().getDomain();
		if (domain != null)
			domain.addPropertyChangeListener(modelListener);

	}

	@Override
	public void deactivate() {
		super.deactivate();
		GraphicalDomain domain = getObjectTemplate().getDomain();
		if (domain != null)
			domain.removePropertyChangeListener(modelListener);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand((String) request.getCellEditor()
						.getValue(), getVariable());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				ITemplateFigure figure = (ITemplateFigure) getFigure();
				figure.setHeadText(getVariable().getName());
				figure.getNameLabel().getUpdateManager().performUpdate();
			}
		};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		ObjectVariableFigure figure = new ObjectVariableFigure();
		figure.setHeadText(this.getObjectTemplate().getName());
		return figure;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)
				|| ModelPackage.VARIABLE_VISIBLE.equals(propertyName)) {
			((ITemplateFigure) getFigure()).setHeadText(getVariable()
					.toString());
		} else if (ModelPackage.VARIABLE_TYPE.equals(propertyName)) {
			if (event.getNewValue() != null) {
				((ITemplateFigure) getFigure()).setHeadText(getVariable()
						.toString());
			}
		} else if (ModelPackage.OBJECT_VARIABLE_EXPRESSION.equals(propertyName)) {
			refreshVisuals();
		} else if (ModelPackage.EXPRESSION_EXP.equals(propertyName)) {
			refreshVisuals();
		} else if (ModelPackage.GRAPHICAL_DOMAIN_COLLAPSE.equals(propertyName)) {
			refreshSourceConnections();
			refreshTargetConnections();
		} else if (ModelPackage.DOMAIN_NEGATIVE.equals(propertyName)
				|| ModelPackage.DOMAIN_CHECKABLE.equals(propertyName)
				|| ModelPackage.DOMAIN_ENFORCEABLE.equals(propertyName)
				|| ModelPackage.DOMAIN_TYPEDMODEL.equals(propertyName)) {

		} else if (ModelPackage.HIGH_LIGHT.equals(propertyName)) {
			((ObjectVariableFigure) getFigure()).hightLight(true);
		} else if (ModelPackage.HIGH_LIGHT_CANCEL.equals(propertyName)) {
			((ObjectVariableFigure) getFigure()).hightLight(false);
		} else {
			super.handlePropertyChanged(event);
		}
	}

	private ObjectVariable getObjectTemplate() {
		return (ObjectVariable) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		((ITemplateFigure) getFigure()).setHeadText(getVariable().toString());

		ObjectVariable modelEle = (ObjectVariable) getNode().getModelElement();
		ObjectVariableFigure figure = (ObjectVariableFigure) getFigure();
		ArrayList<String> propertys = new ArrayList<String>();
		for (Object obj : modelEle.getExpressions()) {
			if (obj instanceof Expression) {
				Expression e = (Expression) obj;
				propertys.add(e.getExp());
			}
		}
		String[] texts = new String[propertys.size()];
	}

	@Override
	protected List getModelSourceConnections() {
		if (getObjectTemplate().getDomain() != null
				&& getObjectTemplate().getDomain().isCollapsed()) {
			return Collections.EMPTY_LIST;
		} else {
			return super.getModelSourceConnections();
		}
	}

	@Override
	public List getModelTargetConnections() {
		if (getObjectTemplate().getDomain() != null
				&& getObjectTemplate().getDomain().isCollapsed()) {
			return Collections.EMPTY_LIST;
		} else {
			return super.getModelTargetConnections();
		}
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return ((ITemplateFigure) getFigure()).getNameLabel();
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE,
				new NestedNodeFlowLayoutPolicy());
	}

	public Variable getVariable() {
		return (Variable) ((MetaNode) this.getModel()).getModelElement();
	}

	@Override
	public IFigure getContentPane() {
		return ((ObjectVariableFigure) getFigure()).getBody();
	}
}
